package com.cily.utils.t_webrtc.singlr.Services;

import com.alibaba.fastjson.JSON;
import com.cily.utils.log.L;
import com.cily.utils.t_webrtc.Conf;
import com.cily.utils.t_webrtc.singlr.Beans.CurrentUser;
import com.cily.utils.t_webrtc.singlr.Beans.MessageBean;
import com.cily.utils.t_webrtc.singlr.Beans.SRUserBean;

/**
 * Created by tim on 2017/8/30.
 */
//instead of StrMsgUtils
public class ActionMessageService {

    public static String login(String userName,String roomNumber) {
        MessageBean messageBean = new MessageBean();
        messageBean.setCode(Conf.ACTION_LOGIN);
        SRUserBean srUserBean = new SRUserBean();
        srUserBean.setUserName(userName);
        srUserBean.setDepartRoom(roomNumber);
        messageBean.setSRUser(srUserBean);

        return JSON.toJSONString(messageBean);
    }

    public static String callVoice(String reciverId,String data){
        return constructMessage(Conf.ACTION_CALL_VIDEO, reciverId,data);
    }

    public static String hold(String reciverId,String data){
        return constructMessage(Conf.ACTION_HOLD, reciverId,data);
    }

    public static String hungup(String reciverId,String data){
        return constructMessage(Conf.ACTION_HUNG_UP, reciverId,data);
    }

    public static String agree(String reciverId,String data){
        return constructMessage(Conf.ACTION_AGREE, reciverId,data);
    }

    public static String refuse(String reciverId,String data){
        return constructMessage(Conf.ACTION_REFUSE, reciverId,data);
    }

    public static String callVedio(String reciverId,String data){

        return constructMessage(Conf.ACTION_CALL_VIDEO, reciverId,data);
    }

    public static String getOnlineUser(String roomNumber){
        return constructMessage(999, "",roomNumber);
    }

    public static String constructMessage(int action, String reciverId,String data) {
        MessageBean messageBean = new MessageBean();
        messageBean.setReciverId(reciverId);
        messageBean.setCode(action);
        messageBean.setData(data);
        messageBean.setSenderId(SignalRService.mConnection.getConnectionId());
        SRUserBean srUserBean = CurrentUser.getCUser();
        messageBean.setSRUser(srUserBean);
        return JSON.toJSONString(messageBean);
    }

    public static MessageBean parseToMessageBean(String message){
        try{
            MessageBean messageBean = com.alibaba.fastjson.JSONObject.parseObject(message, MessageBean.class);
            return messageBean;
        }catch (Exception ex){
            L.printException(ex);
            return null;
        }
    }


}
